package org.example.gui;

import org.example.client.SocketClient;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author WZW
 * @date 2020/7/11
 */
public class SocketClientGui {
    private JTextField jTextFieldIp;
    private JTextField jTextFieldPort;
    private JButton jButtonConnect;
    public static JTextArea jTextAreaLog;

    public void createGui(){
        JFrame jFrame = new JFrame("Socket Client");
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setSize(800,600);
        JPanel jPanelConnect = new JPanel();
        jPanelConnect.setLayout(null);
        jPanelConnect.setSize(800,50);
        jTextFieldIp = new JTextField();
        jTextFieldPort = new JTextField();
        jButtonConnect = new JButton("连接");
        jTextFieldIp.setSize(300,50);
        jTextFieldPort.setSize(300,50);
        jButtonConnect.setSize(200,50);
        jButtonConnect.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String ip = jTextFieldIp.getText();
                String port = jTextFieldPort.getText();
                new Thread(()->{
                    try {
                        new SocketClient().connect(Integer.parseInt(port),ip);
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
                }).start();
            }
        });
        jTextFieldPort.setLocation(300,0);
        jButtonConnect.setLocation(600,0);
        jPanelConnect.add(jTextFieldIp);
        jPanelConnect.add(jTextFieldPort);
        jPanelConnect.add(jButtonConnect);
        jFrame.add(jPanelConnect);
        JPanel jPanelLog = new JPanel();
        jPanelLog.setLayout(null);
        jPanelLog.setSize(800,550);
        jFrame.add(jPanelLog);
        jTextAreaLog = new JTextArea();
        jTextAreaLog.setLocation(20,70);
        jTextAreaLog.setSize(750,500);
        jTextAreaLog.setEditable(true);
        jPanelLog.add(jTextAreaLog);
        jFrame.setLocationRelativeTo(null);
        jFrame.setVisible(true);
    }
}
